"""
登录页面对象
演示页面对象模型的使用
"""

from selenium.webdriver.common.by import By
from .base_page import BasePage


class LoginPage(BasePage):
    """登录页面"""
    
    # 页面元素定位器
    USERNAME_INPUT = (By.ID, "username_input")
    PASSWORD_INPUT = (By.ID, "password_input")
    LOGIN_BUTTON = (By.ID, "login_button")
    FORGOT_PASSWORD_LINK = (By.ID, "forgot_password_link")
    REGISTER_LINK = (By.ID, "register_link")
    ERROR_MESSAGE = (By.ID, "error_message")
    SUCCESS_MESSAGE = (By.ID, "success_message")
    
    def __init__(self, driver_manager):
        super().__init__(driver_manager)
    
    def is_page_loaded(self) -> bool:
        """检查登录页面是否加载完成"""
        return self.is_element_present(*self.USERNAME_INPUT)
    
    def wait_for_page_load(self, timeout: int = 30) -> bool:
        """等待登录页面加载完成"""
        try:
            self.wait_for_element(*self.USERNAME_INPUT, timeout)
            return True
        except:
            return False
    
    def enter_username(self, username: str) -> bool:
        """
        输入用户名
        
        Args:
            username: 用户名
            
        Returns:
            是否成功输入
        """
        return self.input_text(*self.USERNAME_INPUT, username)
    
    def enter_password(self, password: str) -> bool:
        """
        输入密码
        
        Args:
            password: 密码
            
        Returns:
            是否成功输入
        """
        return self.input_text(*self.PASSWORD_INPUT, password)
    
    def click_login_button(self) -> bool:
        """
        点击登录按钮
        
        Returns:
            是否成功点击
        """
        return self.click(*self.LOGIN_BUTTON)
    
    def click_forgot_password(self) -> bool:
        """
        点击忘记密码链接
        
        Returns:
            是否成功点击
        """
        return self.click(*self.FORGOT_PASSWORD_LINK)
    
    def click_register_link(self) -> bool:
        """
        点击注册链接
        
        Returns:
            是否成功点击
        """
        return self.click(*self.REGISTER_LINK)
    
    def get_error_message(self) -> str:
        """
        获取错误信息
        
        Returns:
            错误信息文本
        """
        return self.get_text(*self.ERROR_MESSAGE)
    
    def get_success_message(self) -> str:
        """
        获取成功信息
        
        Returns:
            成功信息文本
        """
        return self.get_text(*self.SUCCESS_MESSAGE)
    
    def is_error_message_displayed(self) -> bool:
        """
        检查是否显示错误信息
        
        Returns:
            是否显示错误信息
        """
        return self.is_element_present(*self.ERROR_MESSAGE)
    
    def is_success_message_displayed(self) -> bool:
        """
        检查是否显示成功信息
        
        Returns:
            是否显示成功信息
        """
        return self.is_element_present(*self.SUCCESS_MESSAGE)
    
    def login(self, username: str, password: str) -> bool:
        """
        执行登录操作
        
        Args:
            username: 用户名
            password: 密码
            
        Returns:
            是否登录成功
        """
        try:
            # 输入用户名
            if not self.enter_username(username):
                print("输入用户名失败")
                return False
            
            # 输入密码
            if not self.enter_password(password):
                print("输入密码失败")
                return False
            
            # 点击登录按钮
            if not self.click_login_button():
                print("点击登录按钮失败")
                return False
            
            return True
            
        except Exception as e:
            print(f"登录操作失败: {e}")
            return False
    
    def clear_login_form(self) -> bool:
        """
        清空登录表单
        
        Returns:
            是否成功清空
        """
        try:
            # 清空用户名输入框
            self.input_text(*self.USERNAME_INPUT, "")
            
            # 清空密码输入框
            self.input_text(*self.PASSWORD_INPUT, "")
            
            return True
            
        except Exception as e:
            print(f"清空登录表单失败: {e}")
            return False 